﻿#include "dxstdafx.h"
#include "ScriptDataMgr.h"
#include "Localisation/Localisation.h"
#include "TerrainID/TerrainIDInfo.h"
#include "TerrainID/TerrainIDInfoIO.h"
#include "TerrainType/TerrainTypeInfo.h"
#include "TerrainType/TerrainTypeInfoIO.h"
#include "LandedTitles/LandedTitles.h"
#include "LandedTitles/LandedTitlesIO.h"
#include "ProvincePositions/ProvincePositions.h"
#include "ProvincePositions/ProvincePositionsIO.h"
#include "Characters/Characters.h"
#include "Characters/CharactersIO.h"

CScriptDataMgr::CScriptDataMgr()
{
	try
	{
		m_pLocalisation = new CLocalisation();
		m_pTerrainTypeInfo = new CTerrainTypeInfo();
		m_pTerrainIDInfo = new CTerrainIDInfo();
		m_pLandedTitlesProperty = new CLandedTitlesProperty();
		m_pProvincePosition = new CProvincePosition();
		m_pCharactersPorperty = new CCharactersPorperty();
	}
	catch (std::bad_alloc &memExp)
	{
		abort();
	}
}

CScriptDataMgr::~CScriptDataMgr()
{
	SAFE_DELETE(m_pLocalisation);
	SAFE_DELETE(m_pTerrainTypeInfo);
	SAFE_DELETE(m_pTerrainIDInfo);
	SAFE_DELETE(m_pLandedTitlesProperty);
	SAFE_DELETE(m_pProvincePosition);
}

void CScriptDataMgr::LoadScript()
{
	CHECK_RETURN(m_pLocalisation);
	m_pLocalisation->ReloadLocalisation();
	
	CHECK_RETURN(m_pTerrainTypeInfo);
	CommonFunc::ReadJsonFile<CTerrainTypeInfoReaderHandle>(L"\\ResFile\\Scene\\Strategy\\terrain_type_info.txt", &m_pTerrainTypeInfo->m_mapTerrainTypeInfo);

	CHECK_RETURN(m_pTerrainIDInfo);
	CommonFunc::ReadJsonFile<CTerrainIDInfoReaderHandle>(L"\\ResFile\\Scene\\Strategy\\terrain_id_info.txt", &m_pTerrainIDInfo->m_mapTerrainIDInfo);

	CHECK_RETURN(m_pCharactersPorperty);
	CommonFunc::ReadJsonFolder<CCharactersReaderHandle>(L"\\ResFile\\Script\\Strategy\\History\\Characters\\", &m_pCharactersPorperty->m_mapCharacterPorperty);
		
	CHECK_RETURN(m_pLandedTitlesProperty);
	CommonFunc::ReadJsonFile<CLandedTitlesReaderHandle>(L"\\ResFile\\Script\\Strategy\\Basic\\landed_titles.txt", &m_pLandedTitlesProperty->m_mapLandedTitlesProperty);

	CHECK_RETURN(m_pProvincePosition);
	CommonFunc::ReadJsonFile<CProvincePositionReaderHandle>(L"\\ResFile\\Scene\\Strategy\\positions.txt", &m_pProvincePosition->m_mapProvincePosition);

	if (!IsDataComplete())
	{
		CString strError("Strategy Data Isn't Complete!");
		CommonFunc::ErrorMessageBox(strError);
	}
}

bool CScriptDataMgr::IsDataComplete() const
{
	CHECK_RE_FALSE(m_pTerrainIDInfo);
	CHECK_RE_FALSE(m_pTerrainTypeInfo);
	CHECK_RE_FALSE(m_pLandedTitlesProperty);
	CHECK_RE_FALSE(m_pProvincePosition);
	CHECK_RE_FALSE(m_pCharactersPorperty);

	return m_pTerrainIDInfo->IsDataComplete() &&
		m_pTerrainTypeInfo->IsDataComplete() &&
		m_pLandedTitlesProperty->IsDataComplete() &&
		m_pProvincePosition->IsDataComplete() &&
		m_pCharactersPorperty->CheckDataCompleteNSortList();
}
